package com.itboye.boyeshoping.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.itboye.boyeshoping.R
import com.itboye.boyeshoping.base.BaseActivity

/**
 * Created by Administrator on 2018/7/12 0012.
 */
class PayRecordActivity :BaseActivity(), View.OnClickListener {


    //title
    private lateinit var backRl: RelativeLayout
    private lateinit var title:TextView
    private lateinit var rightImg:ImageView

    private lateinit var exemptionTv:TextView
    private lateinit var rebateTv:TextView
    private lateinit var line:ImageView
    private lateinit var vp:ViewPager
    private lateinit var emptyRl:RelativeLayout
    private var offset:Int=0//滑动距离
    private var currIndex:Int = 0//当前的page
    private var animaTime:Long=300
    private lateinit var list:List<Fragment>
                 private lateinit var adapter:MyVpAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_paygoodds)
        list=ArrayList()

        initFindView()
        initLine()

        //不是空的话
        emptyRl.visibility=View.GONE

        (list as ArrayList<Fragment>).add(ExemptionFragment())
        (list as ArrayList<Fragment>).add(ExemptionFragment())
        adapter=MyVpAdapter(supportFragmentManager)
        vp.adapter=adapter

        vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

                tranSlate(position,animaTime)
                clearTable()
                if (position==0){
                    exemptionTv.setTextColor(resources.getColor(R.color.maincolor))
                }else{
                    rebateTv.setTextColor(resources.getColor(R.color.maincolor))
                }
                currIndex=position
            }

        })
    }

    override fun onClick(v: View?) {

        when(v!!.id){

            R.id.backRl->{
                finish()
            }
            R.id.exemptionTv->{//免单
                clearTable()
                exemptionTv.setTextColor(resources.getColor(R.color.maincolor))
                vp.currentItem=0
            }

            R.id.rebateTv->{//返利
                 clearTable()
                rebateTv.setTextColor(resources.getColor(R.color.maincolor))
                vp.currentItem=1
            }

        }
    }
    //滑动line初始化
    fun initLine() {
        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val screenW = dm.widthPixels
        val screenH = dm.heightPixels
        offset = screenW / 2
        val params = RelativeLayout.LayoutParams(offset, 2)
        params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.tableLl)
        line.layoutParams = params

    }
    private fun initFindView() {

        backRl=findViewById(R.id.backRl)
        title=findViewById(R.id.title)
        rightImg=findViewById(R.id.rightImg)

        exemptionTv=findViewById(R.id.exemptionTv)
        rebateTv=findViewById(R.id.rebateTv)
        line=findViewById(R.id.line)
        vp=findViewById(R.id.vp)
        emptyRl=findViewById(R.id.emptyRl)

        backRl.setOnClickListener(this)
        exemptionTv.setOnClickListener(this)
        rebateTv.setOnClickListener(this)
        vp.setOnClickListener(this)
    }

    //fragment
    class ExemptionFragment :Fragment(){

        override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            var convertView = inflater!!.inflate(R.layout.fragment_viewpagers, null)

            return convertView
        }

        override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
        }
    }


    //viewpager adapter
    internal inner class MyVpAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment? {
            return list[position]
        }

        override fun getCount(): Int {
            return list.size
        }
    }


    /**
     * 平移动画
     *
     * @param currentItem
     * @param durationMillis
     */
    fun tranSlate(currentItem: Int, durationMillis: Long) {
        val one = offset.toFloat()
        Log.v("offest", "" + offset)
        val animation = TranslateAnimation(currIndex * one,
                currentItem * one, 0f, 0f)// 平移动画
        Log.v("currentoffest", "" + currIndex * one)
        Log.v("currentItem", "" + currentItem * one)
        animation.fillAfter = true// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.duration = durationMillis// 动画持续时间0.2秒
        line.startAnimation(animation)// 是用ImageView来显示动画的
    }

    // hottext 初始化table颜色
    fun clearTable(){
        exemptionTv.setTextColor(resources.getColor(R.color.hottext))
        rebateTv.setTextColor(resources.getColor(R.color.hottext))
    }
}